<?php
namespace App\Admin\Controllers;

use App\Admin\Repositories\BoxConsulting;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Support\JavaScript;
use Illuminate\Support\Str;
use Dcat\Admin\Http\Controllers\AdminController;

class BoxConsultingController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new BoxConsulting(), function (Grid $grid) {
            $grid->quickSearch('title', 'usedatername', 'visits', 'content')->placeholder('搜索...')->auto(true);
            $grid->showColumnSelector();
            $grid->column('id')->sortable()->width('5%');
            $grid->column('title')->width('15%');
            $grid->column('visits')->width('8%');
            $grid->column('date')->width('12%');
            // $grid->column('')->image('http://laraveDact.com/uploads', 50, 50);
            $grid->column('image')->display(function ($avatar) {
                $domain = Str::startsWith($avatar, 'uploads') ? 'http://wxy.jingyi.icu/' : 'http://laraveDact.com/uploads/';
                return "<img data-action='preview-img' src='{$domain}{$avatar}' width='80' height='80' class='img img-thumbnail'>";
            })->width('15%');
            $grid->column('content')->display(function ($content) {
                $limit = 50; // 设置要显示的字符数
                $ellipsis = '...'; // 设置省略号
                if (mb_strlen($content, 'utf-8') > $limit) {
                    $content = mb_substr($content, 0, $limit, 'utf-8') . $ellipsis;
                }
                return $content;
            });
            // ->textarea(false);
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new BoxConsulting(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('visits');
            $show->field('date');
            // $show->field('image')->image('http://laraveDact.com/uploads', 100, 100);
            $show->column('image')->display(function ($avatar) {
                $domain = Str::startsWith($avatar, 'uploads') ? 'http://wxy.jingyi.icu/' : 'http://laraveDact.com/uploads/';
                return "<img data-action='preview-img' src='{$domain}{$avatar}' width='80' height='80' class='img img-thumbnail'>";
            });
            $show->field('content');
            // $show->field('content')->editor('content')->height('200');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new BoxConsulting(), function (Form $form) {
            $form->display('id');
            $form->text('title')->required();
            $form->image('image')->disk('public')->accept('jpg,png,gif,jpeg')->maxSize(512)->required()->autoUpload()->help('大小不要超过512K');
            $form->number('visits')->required();
            $form->date('date')->format('YYYY-MM-DD')->default(date('Y-m-d'));
            // $form->markdown('content')->languageUrl(admin_asset('@admin/dcat/plugins/editor-md/languages/zh-tw.js'));
            $form->editor('content')->height('200')->options([
                'toolbar' => [],
                'setup' => JavaScript::make(
                    <<<JS
                function (editor) {
                    console.log('编辑器初始化成功', editor)
                }
            JS
                ),
            ])->required();
        });
    }
}
